﻿using System.Linq;
using System.Text;
using System.Web;

namespace RTW.Utils.Utils
{
    public static class UrlUtil
    {
        /// <summary>
        /// 本地路径转换成URL相对路径
        /// </summary>
        /// <returns></returns>
        public static string UrlConvertor()
        {
            string url = string.Empty;
            string tmpRootDir = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            var rawUrl = HttpContext.Current.Request.RawUrl;
            string mapPath = rawUrl.Contains("?") ? rawUrl.Substring(0,  rawUrl.IndexOf("?")) : rawUrl;
            string viewPath = HttpContext.Current.Request.MapPath(mapPath);
            string fileUrl = viewPath.Replace(tmpRootDir, ""); //转换成相对路径
            fileUrl = fileUrl.Replace(@"\", @"/");
            if (fileUrl.Contains("/"))
            {
                for (int i = 0; i < fileUrl.Split('/').Count(); i++)
                {
                    url = "../" + url;
                }
            }
            return url.TrimEnd('/');
        }

        /// <summary>
        /// 相对路径转换成服务器本地物理路径
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static string UrlConvertorLocal(string url)
        {
            string tmpRootDir = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string imagesurl2 = tmpRootDir + url.Replace(@"/", @"\"); //转换成绝对路径
            return imagesurl2;
        }

        #region URL处理
        /// <summary>
        /// URL字符编码
        /// </summary>
        public static string UrlEncode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            str = str.Replace("'", "");
            return HttpContext.Current.Server.UrlEncode(str);
        }

        /// <summary>
        /// URL字符解码
        /// </summary>
        public static string UrlDecode(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "";
            }
            return HttpContext.Current.Server.UrlDecode(str);
        }

        /// <summary>
        /// 组合URL参数
        /// </summary>
        /// <param name="_url">页面地址</param>
        /// <param name="_keys">参数名称</param>
        /// <param name="_values">参数值</param>
        /// <returns>String</returns>
        public static string CombUrlTxt(string _url, string _keys, params string[] _values)
        {
            StringBuilder urlParams = new StringBuilder();
            try
            {
                string[] keyArr = _keys.Split(new char[] { '&' });
                for (int i = 0; i < keyArr.Length; i++)
                {
                    if (!string.IsNullOrEmpty(_values[i]) && _values[i] != "0")
                    {
                        _values[i] = UrlEncode(_values[i]);
                        urlParams.Append(string.Format(keyArr[i], _values) + "&");
                    }
                }
                if (!string.IsNullOrEmpty(urlParams.ToString()) && _url.IndexOf("?") == -1)
                    urlParams.Insert(0, "?");
            }
            catch
            {
                return _url;
            }
            return _url + CharUtil.DelLastChar(urlParams.ToString(), "&");
        }
        #endregion
    }
}